package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.controller.utils.DocumentHandler;
import com.ygqh.baby.po.YgSkuMaterial;
import com.ygqh.baby.service.YgSkuMaterialService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/2/14 10:51
 * @since 1.0.0
 */
@Controller
@RequestMapping("admin/skuImport")
public class SkuImportController extends BaseController {


    @Autowired
    private YgSkuMaterialService ygSkuMaterialService;

    @RequestMapping("importCost")
    @ResponseBody
    public Message importCost(@RequestParam MultipartFile uploadFile) {
        Message message;
        try {
            Map<Integer, List<Row>> data = DocumentHandler.importExcel(uploadFile);
            if (data == null) {
                return Message.error("导入模板为空", null);
            }
            List<YgSkuMaterial> list = new ArrayList<>();
            for (Map.Entry<Integer, List<Row>> xssfRows : data.entrySet()) {
                List<Row> rowsValue = xssfRows.getValue();
                Integer sheetNum = xssfRows.getKey();
                for (int i = 0; i < rowsValue.size(); i++) {
                    Row cells = rowsValue.get(i);
                    Cell skuCodeCell = cells.getCell(2);
                    Cell costPriceCell = cells.getCell(4);
                    String skuCode = DocumentHandler.getCellValue(skuCodeCell);
                    String costPrice = DocumentHandler.getCellValue(costPriceCell);
                    if (skuCode == null || costPrice == null) {
                        return Message.error("第" + sheetNum + "个sheet，第" + (i + 1) + "行数据有误", null);
                    }
                    YgSkuMaterial skuMaterial = new YgSkuMaterial();
                    skuMaterial.setSkuCode(skuCode);
                    skuMaterial.setCostPrice(new BigDecimal(costPrice));

                    skuMaterial.setExtMaterial("{\"skuCost\":\"0\"}");
                    list.add(skuMaterial);
                }
            }
            int i = ygSkuMaterialService.updateCostPriceBatch(list, getSession().getTrueName());
            return Message.success("更新成功", i);
        } catch (Exception e) {
            e.printStackTrace();
            message = Message.error(e.getLocalizedMessage(), null);
        }
        return message;
    }

    @RequestMapping("importBarcode")
    @ResponseBody
    public Message importBarcode(@RequestParam MultipartFile uploadFile) {

        Message message;
        try {
            Map<Integer, List<Row>> data = DocumentHandler.importExcel(uploadFile);

            if (data == null) {
                return Message.error("导入模板为空", null);
            }
            List<YgSkuMaterial> list = new ArrayList<>();
            for (Map.Entry<Integer, List<Row>> xssfRows : data.entrySet()) {
                List<Row> rowsValue = xssfRows.getValue();
                Integer sheetNum = xssfRows.getKey();
                for (int i = 0; i < rowsValue.size(); i++) {
                    Row cells = rowsValue.get(i);
                    Cell skuCodeCell = cells.getCell(3);
                    Cell barcodeCell = cells.getCell(4);
                    String skuCode = DocumentHandler.getCellValue(skuCodeCell);
                    String barcode = DocumentHandler.getCellValue(barcodeCell);
                    if (skuCode == null || barcode == null) {
                        return Message.error("第" + (sheetNum + 1) + "个sheet，第" + (i + 1) + "行数据有误", null);
                    }
                    YgSkuMaterial skuMaterial = new YgSkuMaterial();
                    skuMaterial.setSkuCode(skuCode);
                    skuMaterial.setBarCode(barcode);

                    list.add(skuMaterial);
                }
            }
            int i = ygSkuMaterialService.updateBarcodeBatch(list, getSession().getTrueName());
            message = Message.success("更新成功", i);

        } catch (Exception e) {
            e.printStackTrace();
            message = Message.error(e.getLocalizedMessage(), null);
        }
        return message;

    }

}
